BASE_DIR = $(shell pwd -P)
CFG_DIR = $(BASE_DIR)/cfg

-include $(CFG_DIR)/global_vars.mk

LIBS = toxcore ncursesw libconfig

CFLAGS ?= -std=c99 -pthread -Wall -Wpedantic -Wunused -fstack-protector-all -Wvla -Wno-missing-braces
CFLAGS += '-DTOXICVER="$(VERSION)"' -DHAVE_WIDECHAR -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED  -D_FILE_OFFSET_BITS=64
CFLAGS += '-DPACKAGE_DATADIR="$(abspath $(DATADIR))"'
CFLAGS += -O2 -D_FORTIFY_SOURCE=3 -fstack-protector -fstack-protector-all -flto -fPIC -fpic -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro 
# -fsanitize=address,undefined
CFLAGS += -I./src

CFLAGS += ${USER_CFLAGS}
LDFLAGS ?= -flto -fPIE -pie -z now -z relro -z noexecstack -ggdb -lncursesw -lpthread -lconfig -lsodium
LDFLAGS += ${USER_LDFLAGS}

OBJ = autocomplete.o avatars.o bootstrap.o chat.o chat_commands.o conference.o configdir.o execute.o
OBJ += file_transfers.o friendlist.o global_commands.o conference_commands.o groupchats.o groupchat_commands.o help.o
OBJ += input.o line_info.o log.o message_queue.o misc_tools.o notify.o prompt.o settings.o
OBJ += term_mplex.o toxic.o toxic_strings.o windows.o
# Build integrated toxcore with Toxic.
TCSRC += $(wildcard src/toxcore/*.c)
TCSRC += $(wildcard src/toxcore/events/*.c)
TCSRC += $(wildcard src/toxencryptsave/*.c)
TCSRC += $(wildcard src/third_party/cmp/*.c)
TCOBJ = $(TCSRC:.c=.o)
TCOBJX = $(subst src/,,$(TCOBJ))
OBJ += $(TCOBJX)

# Check if debug build is enabled
#RELEASE := $(shell if [ -z "$(ENABLE_RELEASE)" ] || [ "$(ENABLE_RELEASE)" = "0" ] ; then echo disabled ; else echo enabled ; fi)
#ifneq ($(RELEASE), enabled)
#	CFLAGS += -O0 -g -DDEBUG
#	LDFLAGS += -O0
#else
#	CFLAGS += -O2 -flto
#	LDFLAGS += -O2 -flto
#endif

# Check if LLVM Address Sanitizer is enabled
ASAN := $(shell if [ -z "$(ENABLE_ASAN)" ] || [ "$(ENABLE_ASAN)" = "0" ] ; then echo disabled ; else echo enabled ; fi)
ifneq ($(ASAN), disabled)
	CFLAGS += -fsanitize=address -fno-omit-frame-pointer
endif

# Check on wich system we are running
UNAME_S = $(shell uname -s)
ifeq ($(UNAME_S), Linux)
LDFLAGS += -ldl -lrt
endif
ifeq ($(UNAME_S), OpenBSD)
LIBS := $(filter-out ncursesw, $(LIBS))
LDFLAGS += -lncursesw
endif
ifeq ($(UNAME_S), NetBSD)
LIBS := $(filter-out ncursesw, $(LIBS))
LDFLAGS += -lncursesw
endif
ifeq ($(UNAME_S), Darwin)
    -include $(CFG_DIR)/systems/Darwin.mk
endif

# Check on which platform we are running
UNAME_M = $(shell uname -m)
ifeq ($(UNAME_M), x86_64)
    -include $(CFG_DIR)/platforms/x86_64.mk
endif
ifneq ($(filter %86, $(UNAME_M)),)
    -include $(CFG_DIR)/platforms/x86.mk
endif
ifneq ($(filter arm%, $(UNAME_M)),)
    -include $(CFG_DIR)/platforms/arm.mk
endif

# Fix path for object files
OBJ := $(addprefix $(BUILD_DIR)/, $(OBJ))


# Targets
all: $(BUILD_DIR)/toxic

$(BUILD_DIR)/toxic: $(OBJ)
	@echo "  LD    $(@:$(BUILD_DIR)/%=%)"
	@$(CC) $(CFLAGS) -o $(BUILD_DIR)/toxic $(OBJ) $(LDFLAGS)

$(BUILD_DIR)/osx_video.o: $(SRC_DIR)/$(OSX_VIDEO)
	@echo "  CC    $(@:$(BUILD_DIR)/)osx_video.o"
	@$(CC) $(CFLAGS) -o $(BUILD_DIR)/osx_video.o -c $(SRC_DIR)/$(OSX_VIDEO)

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	@if [ ! -e $(BUILD_DIR) ]; then \
		mkdir -p $(BUILD_DIR) ;\
	fi
	@echo "  CC    $(@:$(BUILD_DIR)/%=%)"
	@$(CC) $(CFLAGS) -o $(BUILD_DIR)/$*.o -c $(SRC_DIR)/$*.c
	@$(CC) -MM $(CFLAGS) $(SRC_DIR)/$*.c >$(BUILD_DIR)/$*.d

$(BUILD_DIR)/toxcore/%.o: $(SRC_DIR)/toxcore/%.c
	@if [ ! -e $(BUILD_DIR)/toxcore/ ]; then \
		mkdir -p $(BUILD_DIR)/toxcore/ ;\
		mkdir -p $(BUILD_DIR)/toxcore/events/ ;\
	fi
	@echo "  CC    $(@:$(BUILD_DIR)/%=%)"
	@$(CC) $(CFLAGS) -U_XOPEN_SOURCE -Wno-vla -o $(BUILD_DIR)/toxcore/$*.o -c $(SRC_DIR)/toxcore/$*.c
	@$(CC) -MM $(CFLAGS) -U_XOPEN_SOURCE -Wno-vla $(SRC_DIR)/toxcore/$*.c >$(BUILD_DIR)/toxcore/$*.d

$(BUILD_DIR)/third_party/cmp/%.o: $(SRC_DIR)/third_party/cmp/%.c
	@if [ ! -e $(BUILD_DIR)/third_party/cmp/ ]; then \
		mkdir -p $(BUILD_DIR)/third_party/ ;\
		mkdir -p $(BUILD_DIR)/third_party/cmp/ ;\
	fi
	@echo "  CC    $(@:$(BUILD_DIR)/%=%)"
	@$(CC) $(CFLAGS) -o $(BUILD_DIR)/third_party/cmp/$*.o -c $(SRC_DIR)/third_party/cmp/$*.c
	@$(CC) -MM $(CFLAGS) $(SRC_DIR)/third_party/cmp/$*.c >$(BUILD_DIR)/third_party/cmp/$*.d

$(BUILD_DIR)/toxencryptsave/%.o: $(SRC_DIR)/toxencryptsave/%.c
	@if [ ! -e $(BUILD_DIR)/toxencryptsave/ ]; then \
		mkdir -p $(BUILD_DIR)/toxencryptsave/ ;\
	fi
	@echo "  CC    $(@:$(BUILD_DIR)/%=%)"
	@$(CC) $(CFLAGS) -o $(BUILD_DIR)/toxencryptsave/$*.o -c $(SRC_DIR)/toxencryptsave/$*.c
	@$(CC) -MM $(CFLAGS) $(SRC_DIR)/toxencryptsave/$*.c >$(BUILD_DIR)/toxencryptsave/$*.d


clean:
	rm -f   $(BUILD_DIR)/*.d $(BUILD_DIR)/*.o                                 \
		$(BUILD_DIR)/toxcore/*.o $(BUILD_DIR)/toxcore/*.d                 \
		$(BUILD_DIR)/toxcore/events/*.o $(BUILD_DIR)/toxcore/events/*.d   \
		$(BUILD_DIR)/toxencryptsave/*.o $(BUILD_DIR)/toxencryptsave/*.d   \
		$(BUILD_DIR)/third_party/cmp/*.o $(BUILD_DIR)/third_party/cmp/*.d \
		$(BUILD_DIR)/toxic

-include $(BUILD_DIR)/$(OBJ:.o=.d)

-include $(CFG_DIR)/targets/*.mk

### Static analisys makefile
-include analisys.mk

### Bundles management

bundle:
# TOOD Audit overall project before packing
	git bundle create $(shell git branch --show-current).pack origin/master..HEAD

cleanbundles:
	rm *.pack

.PHONY: clean all
